layui.define(["table", "form", "admin"], function (exports) {
    var $ = layui.$,
        table = layui.table,
        form = layui.form,
        admin = layui.admin;

    table.render({
        elem: "#LAY-matches-match-manage",
        url: "/admin/recommend/lists",
        toolbar: '#toolbaar',
        cols: [
            [
                { type: 'checkbox', fixed: 'left' },
                {
                    field: "match_id",
                    width: 100,
                    title: "比赛ID",
                    sort: true,
                    fixed: "left",
                },
                {
                    field: "",
                    width: 110,
                    title: "比赛类型",
                    sort: true,
                    fixed: "left",
                    templet: function (d) {
                        return $SPORTS[d.sport_id] !== undefined ? $SPORTS[d.sport_id] : "";
                    },
                },
                { field: "comp", title: "所属赛事", width: 200 },
                { field: "home", title: "主队" },
                { field: "away", title: "客队" },
                { field: "match_detail_status", title: "比赛状态", width: 100 },
                {
                    field: "",
                    title: "比赛时间",
                    width: 200,
                    templet: function (d) {
                        return formatTimeStamp(d.match_time);
                    },
                },
                {
                    title: "操作",
                    width: 200,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-match-list",
                },
            ],
        ],
        page: true,
        limit: 50,
        limits: [50, 100, 200, 300, 500],
        height: "full-140",
        text: {
            none: '暂无相关数据！'//默认无数据
        },
        even: true,
    });

    table.on("tool(LAY-matches-match-manage)", function (obj) {
        if (obj.event === "set_flag") {
            layer.confirm("确定设为重要赛事？", function (index) {
                admin.req({
                    url: "/admin/recommend/set_flag",
                    data: { id: obj.data.id, flag: 2 },
                    success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-matches-match-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    },
                });
            });
        } else if (obj.event === "cancel_flag") {
            layer.confirm("确定取消设定重要赛事？", function (index) {
                admin.req({
                    url: "/admin/recommend/set_flag",
                    data: { id: obj.data.id, flag: 1 },
                    success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-matches-match-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    },
                });
            });
        } else if (obj.event === "set_focus") {
            layer.confirm("确定设为焦点赛事？", function (index) {
                admin.req({
                    url: "/admin/recommend/set_focus",
                    data: { id: obj.data.id, focus: 2 },
                    success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-matches-match-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    },
                });
            });
        } else if (obj.event === "cancel_focus") {
            layer.confirm("确定取消焦点赛事？", function (index) {
                admin.req({
                    url: "/admin/recommend/set_focus",
                    data: { id: obj.data.id, focus: 1 },
                    success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-matches-match-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    },
                });
            });
        }
    });

    table.on('toolbar(LAY-matches-match-manage)', function (obj) {
        var id = obj.config.id;
        var ids = table.checkStatus(id);
        var put_ids = '';
        for (let index = 0; index < ids.data.length; index++) {
            put_ids = put_ids + ids.data[index].id + ',';
        }
        if (obj.event == 'set_imp') {
            layer.confirm("确定设为重要赛事？", function (index) {
                admin.req({
                    url: "/admin/recommend/set_ids_flag",
                    data: { put_ids: put_ids },
                    success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-matches-match-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    },
                });
            });
        }

        if (obj.event == 'cancel_imp') {
            layer.confirm("确定取消重要赛事？", function (index) {
                admin.req({
                    url: "/admin/recommend/cancel_ids_flag",
                    data: { put_ids: put_ids },
                    success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-matches-match-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    },
                });
            });
        }
    });

    exports("recommend", {});
});

function formatSeconds(value) {
    var secondTime = parseInt(value); // 秒
    var minuteTime = 0; // 分
    var hourTime = 0; // 小时
    if (secondTime > 60) {
        //如果秒数大于60，将秒数转换成整数
        //获取分钟，除以60取整数，得到整数分钟
        minuteTime = parseInt(secondTime / 60);
        //获取秒数，秒数取佘，得到整数秒数
        secondTime = parseInt(secondTime % 60);
        //如果分钟大于60，将分钟转换成小时
        if (minuteTime > 60) {
            //获取小时，获取分钟除以60，得到整数小时
            hourTime = parseInt(minuteTime / 60);
            //获取小时后取佘的分，获取分钟除以60取佘的分
            minuteTime = parseInt(minuteTime % 60);
        }
    }
    var result =
        "" + parseInt(secondTime) >= 10
            ? parseInt(secondTime)
            : "0" + parseInt(secondTime);

    if (minuteTime > 0) {
        result =
            "" +
            (parseInt(minuteTime) >= 10
                ? parseInt(minuteTime)
                : "0" + parseInt(minuteTime)) +
            ":" +
            result;
    } else {
        result = "" + "00:" + result;
    }

    if (hourTime > 0) {
        result =
            "" +
            (parseInt(hourTime) >= 10
                ? parseInt(hourTime)
                : "0" + parseInt(hourTime)) +
            ":" +
            result;
    } else {
        result = "" + "00:" + result;
    }

    return result;
}

function formatTimeStamp(time_stamp) {
    const time = new Date(time_stamp * 1000);
    const Y = time.getFullYear();
    const M = (time.getMonth() + 1).toString().padStart(2, "0");
    const D = time.getDate().toString().padStart(2, "0");
    const h = time.getHours().toString().padStart(2, "0");
    const m = time.getMinutes().toString().padStart(2, "0");
    const s = time.getSeconds().toString().padStart(2, "0");
    return `${Y}-${M}-${D} ${h}:${m}:${s}`;
}
